package net.wofly.sale.commission.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.ReturnBean;
import net.wofly.sale.commission.domain.CommissionCfg;
import net.wofly.sale.commission.domain.OrderCommission;
import net.wofly.sale.commission.domain.SaleCommissionSum;
import net.wofly.sale.commission.service.ICommissionCfgService;
import net.wofly.sale.commission.validator.CommissionCfgValidator;
import net.wofly.sale.commodity.domain.Commodity;
import net.wofly.sale.commodity.validator.CommodityValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author siqingwei
 */
@RestController
@RequestMapping("/api/commissionCfg")
@Api(description = "佣金配置接口")
public class CommissionCfgController {

	@Autowired
	ICommissionCfgService iCommissionCfgService;
	/**
	 * 添加新的佣金配置
	 *
	 * @param commissionCfg
	 */
	@ApiOperation(value = "添加新的佣金配置")
	@ApiImplicitParam(name = "commissionCfg", value = "佣金配置信息实体", required = true, dataType = "CommissionCfg", paramType = "body")
	@RequestMapping(value = "/addCommissionCfg", method = { RequestMethod.POST })
	ReturnBean<CommissionCfg> addCommissionCfg(@RequestBody CommissionCfg commissionCfg){
		ReturnBean<CommissionCfg> returnBean;

		String errorMessage = CommissionCfgValidator.validateAddCommissionCfgParams(commissionCfg);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iCommissionCfgService.addCommissionCfg(commissionCfg);
		}
		return returnBean;
	}

	/**
	 * 删除佣金配置
	 * <ol>
	 * 	<li>将状态改为无效</li>
	 * 	<li></li>
	 * </ol>
	 *
	 * @param commissionCfgID
	 */
	@ApiOperation(value = "删除佣金配置")
	@ApiImplicitParam(name = "commissionCfgID", value = "佣金配置信息实体ID", required = true, dataType = "String", paramType = "form")
	@RequestMapping(value = "/delCommissionCfg", method = { RequestMethod.POST })
	ReturnBean<CommissionCfg> delCommissionCfg(String commissionCfgID){
		ReturnBean<CommissionCfg> returnBean;

		String errorMessage = CommissionCfgValidator.validateDelCommissionCfgParams(commissionCfgID);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iCommissionCfgService.delCommissionCfg(commissionCfgID);
		}
		return returnBean;
	}

	/**
	 * 获取销售的佣金比率
	 * <ol>
	 * 	<li></li>
	 * </ol>
	 *
	 * @param userID
	 */
	@ApiOperation(value = "获取销售的佣金比率")
	@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String", paramType = "query")
	@RequestMapping(value = "/getCommissionRateByUser", method = { RequestMethod.GET })
	ReturnBean<CommissionCfg> getCommissionRateByUser(String userID){
		ReturnBean<CommissionCfg> returnBean;

		String errorMessage = CommissionCfgValidator.validateGetCommissionRateByUserParams(userID);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iCommissionCfgService.getCommissionRateByUser(userID);
		}
		return returnBean;
	}

	/**
	 * 销售提成流水
	 * <ol>
	 * 	<li>出纳一旦平帐即可计算佣金</li>
	 * 	<li></li>
	 * </ol>
	 *
	 * @param userID
	 * @param startTime
	 * @param endTime
	 * @param pageNum
	 * @param pageSize
	 */
	@ApiOperation(value = "销售提成流水")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userID", value = "用户ID", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "第几页", required = false, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页多少条", required = false, dataType = "int", paramType = "query")
	})
	@RequestMapping(value = "/getSaleCommissionDetail", method = { RequestMethod.GET })
	ReturnBean<PageWrapper<OrderCommission>> getSaleCommissionDetail(String userID, Long startTime, Long endTime, Integer pageNum, Integer pageSize){
		ReturnBean<PageWrapper<OrderCommission>> returnBean;

		String errorMessage = CommissionCfgValidator.validateGetSaleCommissionDetailParams();
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iCommissionCfgService.getSaleCommissionDetail(userID, startTime, endTime, pageNum, pageSize);
		}
		return returnBean;
	}

	@ApiOperation(value = "获取销售提成月汇总")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "sumDuration", value = "统计时长", required = true, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "sumTime", value = "统计所在时间", required = false, dataType = "long", paramType = "query")
	})
	@RequestMapping(value = "/getSaleCommissionMonthSum", method = { RequestMethod.GET })
	ReturnBean<List<SaleCommissionSum>> getSaleCommissionMonthSum(Integer sumDuration, Long sumTime){
		ReturnBean<List<SaleCommissionSum>> returnBean;

		String errorMessage = CommissionCfgValidator.validateGetSaleCommissionMonthSumParams(sumDuration, sumTime);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iCommissionCfgService.getSaleCommissionMonthSum(sumDuration, sumTime);
		}
		return returnBean;
	}

	/**
	 * 修改佣金配置
	 * <ol>
	 * 	<li>修改佣金配置不是直接修改原来记录。而是将原来记录状态改为无效。则新增一条记录</li>
	 * 	<li></li>
	 * </ol>
	 *
	 * @param commissionCfg
	 */
	@ApiOperation(value = "修改佣金配置")
	@ApiImplicitParam(name = "commissionCfg", value = "佣金配置信息实体", required = true, dataType = "CommissionCfg", paramType = "body")
	@RequestMapping(value = "/updateCommissionCfg", method = { RequestMethod.POST })
	ReturnBean<CommissionCfg> updateCommissionCfg(@RequestBody CommissionCfg commissionCfg){
		ReturnBean<CommissionCfg> returnBean;

		String errorMessage = CommissionCfgValidator.validateUpdateCommissionCfgParams(commissionCfg);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iCommissionCfgService.updateCommissionCfg(commissionCfg);
		}
		return returnBean;
	}

}
